package presentation;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.text.DecimalFormat;
import java.util.Observable;
import java.util.Observer;

import org.jfree.chart.plot.ThermometerPlot;
import org.jfree.data.general.DefaultValueDataset;
import org.jfree.ui.RectangleInsets;

public class PThermometer extends ThermometerPlot implements Observer {
	private static final long serialVersionUID = 1L;
	private DefaultValueDataset dataset;
	private DecimalFormat df = new DecimalFormat("#");
	
	public PThermometer(double[] boundary) {
		dataset = new DefaultValueDataset(new Double(0.0));
		
		// Set default values if not given
		if (boundary[0] == 0 && boundary[1] == 0) {
			boundary[1] = 100;
		}
		
		Double warning = (boundary[1]-boundary[0]) * 0.5 + boundary[0];

		Double critical = (boundary[1]-boundary[0]) * 0.75 + boundary[0];
		// create the chart...
		this.setDataset(dataset);
		this.setOutlinePaint(Color.white);
		
		this.setBulbRadius(25);
		this.setColumnRadius(15);
		this.setValuePaint(Color.darkGray);
		this.setValueFont(new Font("Arial",Font.BOLD,18));
		this.setRange(boundary[0], boundary[1]);
        this.setSubrangeInfo(ThermometerPlot.NORMAL, boundary[0], warning.intValue());
        this.setSubrangeInfo(ThermometerPlot.WARNING, warning.intValue(), critical.intValue());
        this.setSubrangeInfo(ThermometerPlot.CRITICAL, critical.intValue(), boundary[1]);
        this.setValueFormat(df);

        this.setThermometerStroke(new BasicStroke(2.0f));
        this.setThermometerPaint(Color.lightGray);
        this.setBackgroundPaint(Color.white);
        this.setInsets(new RectangleInsets(0, 0, 0, 0));
	}
	
	public PThermometer(double[] boundary,Double warning,Double critical) {
		dataset = new DefaultValueDataset(new Double(0.0));
		
		// Set default values if not given
		if (boundary[0] == 0 && boundary[1] == 0) {
			boundary[1] = 100;
		}

		// create the chart...
		this.setDataset(dataset);
		this.setOutlinePaint(Color.white);
		
		this.setBulbRadius(25);
		this.setColumnRadius(15);
		this.setValuePaint(Color.darkGray);
		this.setValueFont(new Font("Arial",Font.BOLD,18));
		this.setRange(boundary[0], boundary[1]);
        this.setSubrangeInfo(ThermometerPlot.NORMAL, boundary[0], warning.intValue());
        this.setSubrangeInfo(ThermometerPlot.WARNING, warning.intValue(), critical.intValue());
        this.setSubrangeInfo(ThermometerPlot.CRITICAL, critical.intValue(), boundary[1]);
        this.setValueFormat(df);

        this.setThermometerStroke(new BasicStroke(2.0f));
        this.setThermometerPaint(Color.lightGray);
        this.setBackgroundPaint(Color.white);
        this.setInsets(new RectangleInsets(0, 0, 0, 0));
	}

	@Override
	public void update(Observable obs, Object arg) {
		if (arg instanceof Number) {
			dataset.setValue( ((Number) arg).doubleValue());
		}
	}
}
